package cn.yuanqiao.cqbc.dataManageTreePath.service.impl;
/**
 * @author 赵常飞
 * @date 2024/6/14 15:56
 */

import cn.yuanqiao.common.core.domain.TreeSelect;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import cn.yuanqiao.feignService.RemoteCommonService;
import cn.yuanqiao.common.core.domain.R;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.cqbc.dataManage.service.DocToOfdService;
import cn.yuanqiao.cqbc.dataManageTreePath.domain.DataManageTreePath;
import cn.yuanqiao.cqbc.dataManageTreePath.mapper.DataManageTreePathMapper;
import cn.yuanqiao.cqbc.dataManageTreePath.service.IDataManageTreePathService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 *@Author: 赵常飞
 *@Date: 2024/6/14 15:56
 *@Description:
 *@Note:
 */
@Service
public class IDataManageTreePathServiceImpl implements IDataManageTreePathService {
    @Autowired
    private DataManageTreePathMapper dataManageTreeMapper;
    @Autowired
    private RemoteCommonService remoteCommonService;
    @Autowired
    private DocToOfdService docToOfdService;

    @Override
    public List<TreeSelect> getTreeList() {
        R<List<TreeSelect>> resultR=remoteCommonService.getdanganTree();
        return resultR.getData();
    }

    @Override
    public String addPath(DataManageTreePath dataManageTreePath) {
        LoginUser user = SecurityUtils.getLoginUser();
        if(user == null){
            user = new LoginUser();
            user.setUserId(1L);
        }
        //同一门类 path不能相同
        dataManageTreePath.setTenantCode(SecurityUtils.getLoginUser().getSysUser().getTenantCode());
        dataManageTreePath.setState(null);
        DataManageTreePath i=dataManageTreeMapper.selectpath(dataManageTreePath);
        if(null!=i){
            return "地址重复!";
        }
        dataManageTreePath.setId(SnowflakeIdGenerator.generateId());
        dataManageTreePath.setState(0);
        dataManageTreePath.setCreator(user.getUserId());
        dataManageTreePath.setCreatTime(String.valueOf(DateUtils.getNowDate()));
        dataManageTreeMapper.addPath(dataManageTreePath);
        return "ok";
    }

    @Override
    public List<DataManageTreePath> getPath(Long categoryId) {
        DataManageTreePath dataManageTreePath=new DataManageTreePath();
        dataManageTreePath.setTenantCode(SecurityUtils.getLoginUser().getSysUser().getTenantCode());
        dataManageTreePath.setCategoryId(categoryId);
        return dataManageTreeMapper.getPath(dataManageTreePath);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateEnable(DataManageTreePath DataManageTreePath) {
        DataManageTreePath.setState(0);
        dataManageTreeMapper.updateAll(DataManageTreePath);
        DataManageTreePath.setState(1);
        return dataManageTreeMapper.update(DataManageTreePath);
    }

    @Override
    public String deletePath(Long pathId) {
        //是否已经使用
        String tenantCode=SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        int i=dataManageTreeMapper.finddatabyPathId(pathId,tenantCode);
        if (i>0){
            return "此地址已经被使用,无法删除!";
        }else{
            dataManageTreeMapper.deletePath(pathId);
        }
        return "ok";
    }

    @Override
    public String update(DataManageTreePath dataManageTreePath) {
        if(null!=dataManageTreePath.getPath()){
            //判断地址是否已经使用
            String tenantCode=SecurityUtils.getLoginUser().getSysUser().getTenantCode();
            int i=dataManageTreeMapper.finddatabyPathId(dataManageTreePath.getId(),tenantCode);
            if (i>0) {
                return "此地址已经被使用,无法删除!";
            }else{
                dataManageTreeMapper.update(dataManageTreePath);
            }
        }else{
            dataManageTreeMapper.update(dataManageTreePath);
        }

        return null;
    }

    @Override
    public List<Map<String,Object>> getList(JSONObject json) {
        Long pathId=json.getLong("pathId");
        String zlfs=json.getString("zlfs");
        String fields=json.getString("fields");
        List<Map<String,Object>> dataList=new ArrayList<>();
        String tenantCode=SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        if("0".equals(zlfs)){
            dataList=dataManageTreeMapper.getdataList("select "+fields+" from "+tenantCode+"_CQBC_HDDA where PATH_ID="+pathId);
        }else if("1".equals(zlfs)){
            dataList=dataManageTreeMapper.getdataList("select "+fields+" from "+tenantCode+"_CQBC_FOLDER where PATH_ID="+pathId);
        }else if("3".equals(zlfs)){
            dataList=dataManageTreeMapper.getdataList("select "+fields+" from "+tenantCode+"_CQBC_ITEM where PATH_ID="+pathId);
        }
        return null;
    }

    @Override
    public List<Map<String, String>> getshowFields(JSONObject json) {
        R<List<Map<String, String>>> listR=remoteCommonService.getshowFields(json);
        return listR.getData();
    }

    @Override
    public void trans(JSONObject json) throws Exception {
        docToOfdService.docToOfd(json);
    }


    public DataManageTreePath selectpath(DataManageTreePath dataManageTreePath) {

        return dataManageTreeMapper.selectpath(dataManageTreePath);
    }
    @Override
    public List<Map<String, String>> getQueryFields(JSONObject json) {
        R<List<Map<String, String>>> listR=remoteCommonService.getQueryFields(json);
        return listR.getData();
    }
}
